package com.l024.easyandroidlib.log.provider;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.l024.easyandroidlib.log.util.EasyLogDisplayUtil;


/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/29 10:17
 * @Notes
 */
public class EassyLogViewPrinterProvider {
    private FrameLayout rootView;
    private View floatingView;
    private boolean isOpen;
    private FrameLayout logView;
    private RecyclerView recyclerView;
    private static final String TAG_FLOATING_VIEW = "TAG_FLOATING_VIEW";
    private static final String TAG_LOG_VIEW = "TAG_LOG_VIEW";

    public EassyLogViewPrinterProvider(FrameLayout rootView, RecyclerView recyclerView) {
        this.rootView = rootView;
        this.recyclerView = recyclerView;
    }

    //显示浮动的view
    public void showFloatingView(){
        //根据view的tag查询
        if(rootView.findViewWithTag(TAG_FLOATING_VIEW)!=null){
            return;
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM|Gravity.END;
        View floatingView = genFloatingView();
        floatingView.setTag(TAG_FLOATING_VIEW);
        floatingView.setBackgroundColor(Color.BLACK);
        floatingView.setAlpha(0.8f);
        params.bottomMargin = EasyLogDisplayUtil.dp2px(100,recyclerView.getResources());
        rootView.addView(floatingView,params);
    }

    /**
     * 关闭Log悬浮按钮
     * @return
     */
    public void closeFloatingView(){
        rootView.removeView(genFloatingView());
    }

    private View genFloatingView() {
        if(floatingView!=null){
            return floatingView;
        }
        TextView textView = new TextView(rootView.getContext());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!isOpen){
                    closeFloatingView();
                    showLogView();
                }
            }
        });
        textView.setText("EasyLog");
        textView.setPadding(10,5,10,5);
        textView.setTextColor(Color.WHITE);
        return floatingView=textView;
    }

    private void showLogView(){
        if(rootView.findViewWithTag(TAG_LOG_VIEW)!=null){
            return;
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, EasyLogDisplayUtil.dp2px(160,rootView.getResources()));
        params.gravity = Gravity.BOTTOM;
        View logView = getLogView();
        logView.setTag(TAG_LOG_VIEW);
        rootView.addView(logView,params);
        isOpen = true;
    }

    private View getLogView() {
        if(logView!=null){
            return logView;
        }
        FrameLayout logView = new FrameLayout(rootView.getContext());
        logView.setBackgroundColor(Color.BLACK);
        logView.addView(recyclerView);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.END;
        final TextView closeView = new TextView(rootView.getContext());
        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFloatingView();
                closeLogView();
            }
        });
        closeView.setText("Close");
        closeView.setTextColor(Color.WHITE);
        closeView.setPadding(10,5,10,5);
        logView.addView(closeView,params);
        return this.logView=logView;
    }

    /**
     * 关闭日志
     */
    private void closeLogView() {
        isOpen = false;
        rootView.removeView(getLogView());
    }
}
